﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using CTS.Com.Domain.Helper;
using CTS.Com.Domain.Logic;
using CTS.Com.Domain.Model;
using CTS.Web.Com.Domain.Helper;
using CTS.Web.Com.Domain.Utils;

namespace CTS.Web.Com.Domain.Controller
{
    public abstract class MasterPageBase : MasterPage, IDisposable
    {
        public static AjaxCom Ajax { get { return new AjaxCom(); } }

        public override void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        protected string GetReturnUrl(string url)
        {
            return string.Format("{0}?returnUrl={1}", url, Request.Url.PathAndQuery);
        }

        protected void RedirectWithReturnUrl(string url)
        {
            Response.Redirect(GetReturnUrl(url));
        }

        protected T Invoke<T>(IOperateLogic<T> logic, object param) where T : BasicResponse, new()
        {
            return PageHelper.Invoke<T>(logic, param);
        }

        protected T Invoke<T>(IOperateLogic<T> logic, object param, Type paramType) where T : BasicResponse, new()
        {
            return PageHelper.Invoke<T>(logic, param, paramType);
        }

        protected T CopyValue<T>(object data) where T : new()
        {
            return DataHelper.CopyObject<T>(data);
        }

        protected bool IsNull(string data)
        {
            return DataCheckHelper.IsNull(data);
        }

        protected object GetSession(string name)
        {
            return Page.Session[name];
        }

        protected decimal ToDecimal(string data)
        {
            var value = DataHelper.ConvertInputNumber(data);
            return Convert.ToDecimal(value);
        }

        protected decimal ToDecimal(decimal? data)
        {
            return Convert.ToDecimal(data);
        }

        protected string ToString(bool? data)
        {
            return DataHelper.ToString(data);
        }

        protected string ToString(object data)
        {
            return Convert.ToString(data);
        }

        protected IList<object> ToList(object data)
        {
            return new List<object>() { data };
        }

        protected ComboItem ToComboItem(object data)
        {
            return DataHelper.ToComboItem(data);
        }

        protected string ToSubString(string data, int size)
        {
            return DataHelper.ToSubString(data, size);
        }

        /****************************** For Ajax ******************************/

        protected static T InvokeInAjax<T>(IOperateLogic<T> logic, object param) where T : BasicResponse, new()
        {
            return PageHelper.Invoke<T>(logic, param);
        }

        protected static T InvokeInAjax<T>(IOperateLogic<T> logic, object param, Type paramType) where T : BasicResponse, new()
        {
            return PageHelper.Invoke<T>(logic, param, paramType);
        }

        protected static object GetSessionInAjax(string name)
        {
            return HttpContext.Current.Session[name];
        }
    }
}